﻿using RotovapPlatform.Domain.Equipment.Interfaces.Base;
using System.Threading.Tasks;

/// <summary>
/// 串口服务器管理器接口
/// </summary>
namespace RotovapPlatform.Domain.Equipment.Interfaces.Communication
{
	public interface ISerialServerManager
	{
		/// <summary>
		/// 获取整体连接状态，至少一个端口连接成功则为true
		/// </summary>
		bool IsConnected { get; }

		/// <summary>
		/// 获取指定端口的连接状态
		/// </summary>
		/// <param name="port">端口号</param>
		/// <returns>端口是否已连接</returns>
		bool IsPortConnected(int port);

		/// <summary>
		/// 连接到串口服务器
		/// </summary>
		Task<bool> ConnectAsync();

		/// <summary>
		/// 断开连接
		/// </summary>
		Task DisconnectAsync();

		/// <summary>
		/// 检查所有连接状态，并尝试重连断开的连接
		/// </summary>
		/// <returns>如果所有预期的连接都正常，则返回true，否则返回false</returns>
		Task<bool> CheckConnectionAsync();

		/// <summary>
		/// 读取保持寄存器
		/// </summary>
		/// <param name="devicePort">设备端口号</param>
		/// <param name="slaveId">从站地址</param>
		/// <param name="startAddress">起始地址</param>
		/// <param name="length">读取长度</param>
		Task<ushort[]> ReadHoldingRegistersAsync(int devicePort, byte slaveId, ushort startAddress, ushort length);

		/// <summary>
		/// 写入保持寄存器
		/// </summary>
		/// <param name="devicePort">设备端口号</param>
		/// <param name="slaveId">从站地址</param>
		/// <param name="startAddress">起始地址</param>
		/// <param name="values">要写入的值</param>
		Task WriteHoldingRegistersAsync(int devicePort, byte slaveId, ushort startAddress, ushort[] values);

		/// <summary>
		/// 读取线圈状态
		/// </summary>
		/// <param name="devicePort">设备端口号</param>
		/// <param name="slaveId">从站地址</param>
		/// <param name="startAddress">起始地址</param>
		/// <param name="length">读取长度</param>
		Task<bool[]> ReadCoilsAsync(int devicePort, byte slaveId, ushort startAddress, ushort length);

		/// <summary>
		/// 写入线圈
		/// </summary>
		/// <param name="devicePort">设备端口号</param>
		/// <param name="slaveId">从站地址</param>
		/// <param name="startAddress">起始地址</param>
		/// <param name="values">要写入的值</param>
		Task WriteCoilsAsync(int devicePort, byte slaveId, ushort startAddress, bool[] values);

		/// <summary>
		/// 发送自定义命令
		/// </summary>
		/// <param name="devicePort">设备端口号</param>
		/// <param name="command">命令</param>
		/// <param name="expectedResponseLength">预期响应长度</param>
		/// <param name="timeoutMs">超时时间（毫秒）</param>
		Task<byte[]> SendCustomCommandAsync(int devicePort, byte[] command, int expectedResponseLength, int timeoutMs = 1000);
	}
}